﻿using System.Collections.Generic;
using System.Windows.Input;
using Socium.Domain;
using Socium.Domain.Collections;
using Socium.Domain.Tests.Testings;
using Socium.Presentation;

namespace Socium.Testings {
	class TestingFormViewModel : FormViewModel<Testing> {
		readonly Database _database;
		readonly ISelectionSource _selection_source;

		readonly TestingTestsIndexViewModel _tests;

		public TestingFormViewModel(Testing model, Database database, ISelectionSource selection_source) : base(model) {
			_database = database;
			_selection_source = selection_source;
			
			_tests = new TestingTestsIndexViewModel("Tests", database, selection_source, model.Tests.MapLazy(test => new TestLineItem(test)));
		}

		public TestingFormViewModel(Database database, ISelectionSource selection_source) {
			_database = database;
			_selection_source = selection_source;

			_tests = new TestingTestsIndexViewModel("Tests", database, selection_source);
		}

		public ICommand AddTestCommand {
			get { return _tests.NewItemCommand; }
		}

		protected override Testing CreateModel() {
			return new Testing();
		}

		protected override void Update(Testing model) {
			// ...
		}

		public override bool CanSave() {
			return !_tests.IsEmpty;
		}

		protected override void Save(Testing model) {
			_tests.Save(model.Tests);
		}

		protected override string GetDisplayName(Testing model) {
			return model.Date.ToString();
		}

		protected override string GetDefaultDisplayName() {
			return "New testing";
		}

		public IEnumerable<TestLineItem> Tests {
			get { return _tests.Childeren; }
		}
	}
}
